<template>
  <div id="partakeGame">
    <app-header></app-header>
    <app-marquee v-if="noticeInfo" :title="noticeInfo"/>
    <div class="auto1200 tab_msg">
      <ul>
        <li v-if="currencyType&&currencyType.length>0" v-for="(list,index) in currencyType" :key="index"
            :class="currentId===list.id?'li_active':''" @click="handleCurrencydata(list)">{{list.name}}
        </li>
        <!--<li class="li_active">ETH</li>-->
        <!--<li>BTC</li>-->
      </ul>
      <p>账户余额不足？<span @click="goAssest">去充币</span></p>
    </div>
    <div class="auto1200 main_session">
      <div class="main_left">
        <dl v-if="listData&&listData.length>0" v-for="(item,index) in listData" :key="'list_'+index">
          <dd :class="item.touzhuValid?'dd_status2':'dd_status1'"></dd>
          <dd>
            <div class="title">
              <div class="status status1"></div>
              <div class="main_text">
                <span>{{item.name}}</span>
                <p>{{item.rule}}</p>
              </div>
            </div>
            <div class="main_dec">
              <span>期数：第{{item.version}}期 <b @click="handleVisible">查看投注记录<i class="el-icon-arrow-right"></i></b></span>
              <span>最小投注额：{{minAmount[index].minAmount}}{{currentName}}</span>
              <span>开始时间：{{item.startTime|timeFormatting}}</span>
            </div>
            <div class="progress_box">
              <div class="progress">
                <span :style="{'width':item.currentTotal/item.maxTotal*100+'%'}"></span>
              </div>
              <div class="progress_number">
                <span>{{item.currentTotal}}</span>
                /{{item.maxTotal}}
              </div>
            </div>
          </dd>
          <dt class="check_number">
            <i class="iconfont" @click="handleDelete(index)">&#xe608;</i>
            <input type="text" placeholder="投注金额" :value="item.minAmount+currentName">
            <i class="iconfont" @click="handleAdd(index)">&#xe641;</i>
            <div class="button btn_active" @click="handleBetting(index)">投注</div>
          </dt>
          <dt class="winners" v-if="item.touzhuValid">
            <span>中奖者:{{item.winnerName}}</span>
            <p>距下期开始还剩：<span>{{item.backTime}}</span></p>
          </dt>
          <dt class="winners" v-if="!item.touzhuValid">
            <span>上期中奖者:{{item.winnerName}}</span>
            <p>上期中奖号码：<span>{{item.backTime}}</span></p>
          </dt>
        </dl>
        <!--<dl>-->
        <!--<dd class="dd_status2"></dd>-->
        <!--<dd>-->
        <!--<div class="title">-->
        <!--<div class="status status2"></div>-->
        <!--<div class="main_text">-->
        <!--<span>100 ETH</span>-->
        <!--<p>10000倍收益/0.01ETH起投</p>-->
        <!--</div>-->
        <!--</div>-->
        <!--<div class="main_dec">-->
        <!--<span>期数：第一期</span>-->
        <!--<span>最小投注额：0.01ETH</span>-->
        <!--<span>开始时间：2018-11-04 20:25:10</span>-->
        <!--</div>-->
        <!--<div class="progress_box">-->
        <!--<div class="progress">-->
        <!--<span></span>-->
        <!--</div>-->
        <!--<div class="progress_number">-->
        <!--<span>53.23</span>-->
        <!--/100.00-->
        <!--</div>-->
        <!--</div>-->
        <!--</dd>-->
        <!--<dt class="check_number">-->
        <!--<i class="iconfont">&#xe608;</i>-->
        <!--<input type="text">-->
        <!--<i class="iconfont">&#xe641;</i>-->
        <!--<div class="button ">投注</div>-->
        <!--</dt>-->
        <!--<dt class="winners">-->
        <!--<span>中奖者:-&#45;&#45;</span>-->
        <!--<p>距下期开始还剩：<span>23分53秒</span></p>-->
        <!--</dt>-->
        <!--</dl>-->
        <!--<dl>-->
        <!--<dd class="dd_status1"></dd>-->
        <!--<dd>-->
        <!--<div class="title">-->
        <!--<div class="status status3"></div>-->
        <!--<div class="main_text">-->
        <!--<span>100 ETH</span>-->
        <!--<p>10000倍收益/0.01ETH起投</p>-->
        <!--</div>-->
        <!--</div>-->
        <!--<div class="main_dec">-->
        <!--<span>期数：第一期</span>-->
        <!--<span>最小投注额：0.01ETH</span>-->
        <!--<span>开始时间：2018-11-04 20:25:10</span>-->
        <!--</div>-->
        <!--<div class="progress_box">-->
        <!--<div class="progress">-->
        <!--<span></span>-->
        <!--</div>-->
        <!--<div class="progress_number">-->
        <!--<span>53.23</span>-->
        <!--/100.00-->
        <!--</div>-->
        <!--</div>-->
        <!--</dd>-->
        <!--<dt class="check_number">-->
        <!--<i class="iconfont">&#xe608;</i>-->
        <!--<input type="text">-->
        <!--<i class="iconfont">&#xe641;</i>-->
        <!--<div class="button btn_active">投注</div>-->
        <!--</dt>-->
        <!--<dt class="winners">-->
        <!--<span>中奖者:-&#45;&#45;</span>-->
        <!--<p>距下期开始还剩：<span>23分53秒</span></p>-->
        <!--</dt>-->
        <!--</dl>-->
      </div>
      <div class="main_right">
        <img src="../assets/image/banner_min.png" alt="">
        <div class="bonusList">
          <div class="nav">
            <span>奖金</span>
            <span>中奖人</span>
            <span>时间</span>
          </div>
          <div class="main_list" id="main_list">
            <div class="main_list_item" v-for="(item,index) in awardList" :key="'list_'+index">
              <span>{{item.award}}</span>
              <span>{{item.userName}}</span>
              <span class="span_date">{{item.createTime | monthFormatting}}</span>
            </div>
            <div class="main_list_item" v-if="!awardList||awardList.length<1">
              <p class="none-data">暂无数据</p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <betting-detail :visible="visibleFlag"/>
  </div>
</template>

<script>
  import header from '../components/header/header'
  import marquee from '../components/marquee/marquee'
  import Bettingdetail from './bettingDetail'
  import Api from '@/axios/api/api'

  export default {
    name: 'partakeGame',
    data() {
      return {
        oIndex: 0,
        visibleFlag: false,
        currencyType: [
          {
            'id': 1,
            'name': 'BTC'
          },
          {
            'id': 3,
            'name': 'ETH'
          },
          {
            'id': 5,
            'name': 'USDT'
          }
        ],
        currentId: 1,
        currentName: 'BTC',
        listData: [],
        awardList: [],
        minAmount: [],
        timer: null,
        scrollFalg: true,
        noticeInfo: ''
      }
    },
    created() {

    },
    methods: {
      goAssest() {
        this.$router.push({
          path: '/assets'
        })
      },
      getCurrency() {
        Api.getCurrencytype().then(res => {
          let result = res.data
          if (result.code === 0) {
            this.currencyType = []
            this.currencyType = result.data
            this.currentId = result.data[0].id
          } else {
            this.$message.error(result.message)
          }
        })
      },
      getList() {
        Api.getRacelist().then(res => {
          let result = res.data
          this.listData = []
          if (result.code === 0) {
            this.listData = result.data
            this.handleTick()
            for (let index in this.listData) {
              this.minAmount.push({
                minAmount: this.listData[index].minAmount
              })
            }
          } else {
            this.$message.error(result.message)
          }
        })
      },
      getNoticebar() {
        Api.noticeBar().then(res => {
          let result = res.data
          if (result.code === 0) {
            this.noticeInfo = result.data.message
          } else {
            this.$message.error(result.message)
          }
        })
      },
      getAwardlist() {
        Api.pastWinnerslist().then(res => {
          let result = res.data
          if (result.code === 0) {
            this.awardList = result.data
          } else {
            this.$message.error(result.message)
          }
        })
      },
      handleCurrencydata(data) {
        this.currentId = data.id
        this.currentName = data.name
      },
      handleAdd(index) {
        this.listData[index].minAmount = this.listData[index].minAmount + this.minAmount[index].minAmount
      },
      handleDelete(index) {
        if (this.listData[index].minAmount === this.minAmount[index].minAmount) {
          this.$message.error('投注不能小于目标最小投资数！')
          return false
        }
        this.listData[index].minAmount = this.listData[index].minAmount - this.minAmount[index].minAmount
      },
      handleBetting(index) {
        if (this.listData[index].minAmount < this.minAmount[index].minAmount) {
          this.$message.error('投注不能小于目标最小投资数！')
          return false
        }
        let param = {
          raceTypeM: this.listData[index].id,
          amount: this.listData[index].minAmount
        }
        Api.gameBetting(param).then(res => {
          let result = res.data
          if (result.code === 0) {
            this.listData[index].currentTotal = result.data.currentTotal
            this.$message.success('投注成功')
            if (result.data.end) {
              setTimeout(() => {
                this.getList()
              }, 2000)
            }
          } else {
            this.$message.error(result.message)
          }
        })
      },
      handleTick() {
        clearInterval(this.timer)
        this.timer = setInterval(() => {
          if (this.listData.length > 0) {
            for (let index in this.listData) {
              let currentTime = this.countDown(this.listData[index].startTime)
              if (currentTime) {
                this.listData[index].backTime = currentTime
              }
            }
          }
        }, 1000)
      },
      toDou(num) {
        return num > 10 ? '' + num : '0' + num
      },
      countDown(value) {
        if (!value) return false
        let oTarget = new Date(value).getTime()
        let oDate = new Date()
        let s = parseInt((value - oDate.getTime()) / 1000)
        let countDownText = null
        if (s > 0) {
          let d = parseInt(s / 86400)
          s %= 86400
          let h = parseInt(s / 3600)
          s %= 3600
          let m = parseInt(s / 60)
          s %= 60
          if (d > 0) {
            countDownText = (d * 24) + h + ':' + this.toDou(m) + ':' + this.toDou(s)
          } else {
            countDownText = this.toDou(h) + ':' + this.toDou(m) + ':' + this.toDou(s)
          }
        } else {
          // this.getList()  倒计时重新获取数据
        }
        return countDownText
      },
      handleVisible() {
        this.visibleFlag = !this.visibleFlag
      }
    },
    components: {
      'app-header': header,
      'app-marquee': marquee,
      'betting-detail': Bettingdetail
    },
    mounted() {
      this.getList()
      this.getAwardlist()
      this.getNoticebar()
      // let vm = this
      // // scrollFalg
      // let oWinning = document.getElementById('main_list')
      // if (oWinning) {
      //   oWinning.addEventListener('scroll', function () {
      //     if (oWinning.scrollTop + oWinning.innerHeight >= oWinning.offsetHeight) {
      //       alert(1)
      //     }
      //   })
      // }
    }
  }
</script>

<style lang="less">
  #partakeGame {
    /*tab*/
    .tab_msg {
      height: 60px;
      padding-bottom: 45px;
      line-height: 60px;
      font-family: PingFangSC-Medium;
      ul {
        overflow: hidden;
        float: left;
        li {
          margin-right: 60px;
          position: relative;
          float: left;
          cursor: pointer;
          &:hover {
            color: #5A27F6;
          }
        }
        .li_active {
          color: #5A27F6;
          &:after {
            position: absolute;
            content: '';
            width: 100%;
            height: 2px;
            background: #5A27F6;
            left: 0;
            bottom: 0;
          }
        }
      }
      p {
        float: right;
        font-size: 18px;
        color: #210B00;
        span {
          color: #5117FD;
          cursor: pointer;
          &:hover {
            opacity: 0.8;
          }
          &:active {
            position: relative;
            top: 1px;
          }
        }
      }
    }
    .main_session {
      .main_left {
        dl {
          border-radius: 2px;
          width: 320px;
          padding: 24px 19px 18px 21px;
          position: relative;
          font-family: PingFangSC-Semibold;
          float: left;
          margin-right: 30px;
          box-shadow: 2px 2px 10px 0 #DBD9DF;
          margin-bottom: 30px;
          &:nth-child(2n+2) {
            margin-right: 0;
          }
          .dd_status1 {
            position: absolute;
            width: 102px;
            height: 102px;
            right: 0;
            top: 0;
            background: url("../assets/image/gamestatus1.png") no-repeat;
            background-size: 100% 100%;
          }
          .dd_status2 {
            position: absolute;
            width: 102px;
            height: 102px;
            right: 0;
            top: 0;
            background: url("../assets/image/gamestatus2.png") no-repeat;
            background-size: 100% 100%;
          }
        }
        .winners {
          padding-top: 30px;
          span {
            font-size: 16px;
            color: #000000;
            letter-spacing: 0;
            line-height: 28px;
          }
          p {
            span {
              color: #5A27F6;
            }
          }
        }
        .check_number {
          padding-top: 28px;
          overflow: hidden;
          > i {
            font-size: 20px;
            line-height: 36px;
            cursor: pointer;
            padding: 0 5px;
          }
          input {
            width: 130px;
            height: 36px;
            border: 1px solid #A9A9A9;
            border-radius: 4px;
            box-sizing: border-box;
            text-indent: 6px;
          }
          .button {
            background: #D8D8D8;
            border-radius: 4px;
            width: 88px;
            height: 36px;
            border: none;
            line-height: 36px;
            text-align: center;
            color: #fff;
            float: right;
            position: relative;
            top: 2px;
            cursor: pointer;
            &:hover {
              opacity: 0.8;
            }
            &:active {
              top: 3px;
            }
          }
          .btn_active {
            background: #5A27F6 !important;
          }
        }
        dd {
          .progress {
            width: 208px;
            height: 24px;
            position: relative;
            background: #D8D8D8;
            border-radius: 12px;
            float: left;
            span {
              position: absolute;
              width: 30%;
              height: 100%;
              border-radius: 12px;
              background: #5A27F6;
            }
          }
          .progress_box {
            height: 24px;
            width: 100%;
            overflow: hidden;
          }
          .progress_number {
            float: right;
            font-size: 15px;
            color: #000;
            letter-spacing: 0;
            line-height: 24px;
            span {
              color: #5A27F6;
            }
          }
          .title {
            overflow: hidden;
            .status1 {
              background: url("../assets/image/status01.png") no-repeat;
              background-size: 100% 100%;
            }
            .status2 {
              background: url("../assets/image/status02.png") no-repeat;
              background-size: 100% 100%;
            }
            .status3 {
              background: url("../assets/image/status03.png") no-repeat;
              background-size: 100% 100%;
            }
            .status {
              float: left;
              width: 80px;
              height: 80px;
            }
            .main_text {
              float: left;
              margin-left: 16px;
              span {
                font-size: 32px;
                color: #5A27F6;
                line-height: 50px;
              }
              p {
                font-size: 18px;
                color: #7B52F8;;
                height: 50px;
              }
            }
          }
          .main_dec {
            padding-bottom: 20px;
            span {
              font-size: 16px;
              color: #000000;
              letter-spacing: 0;
              line-height: 28px;
              display: block;
              > b {
                font-size: 14px;
                color: #5A27F6;
                cursor: pointer;
                &:hover {
                  opacity: 0.8;
                }
                &:active {
                  position: relative;
                  top: 1px;
                }
              }
            }
          }
        }
      }
      .bonusList {
        box-shadow: 2px 2px 10px 0 #DBD9DF;
        .nav {
          background: #5A27F6;
          color: #fff;
          line-height: 56px;
          overflow: hidden;
          span {
            font-size: 18px;
            width: 33.33%;
            float: left;
            text-align: center;
            overflow: hidden;
          }
        }
        .main_list {
          padding: 0 24px;
          max-height: 588px;
          overflow: hidden;
          /* margin-bottom: 14px; */
          /*height: 261px;*/
          .main_list_item {
            font-size: 16px;
            color: #000;
            line-height: 48px;
            /*-webkit-box-sizing: border-box;*/
            /*box-sizing: border-box;*/
            border-bottom: 1px solid #eee;
            overflow: hidden;
            &:last-child {
              border-bottom: none;
            }
            span {
              float: left;
              width: 33.33%;
              display: block;
              text-align: center;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
              &:nth-child(1) {
                color: #7B52F8;
                font-size: 15px;
              }
            }
            .span_date {
              text-align: right;
            }
          }
        }
      }
    }
    .auto1200 {
      .main_left {
        height: 860px;
      }
      .main_right {
        height: 860px;
        > img {
          width: 400px;
          height: 200px;
          display: block;
          margin-bottom: 40px;
        }
      }
    }
    .marquee {
      width: 100%;
      height: 60px;
      line-height: 60px;
      opacity: 0.89;
      background: #7820FD;
      color: #fff;
      img {
        position: absolute;
        width: 30px;
        top: 15px;
        left: -1px;
      }
      marquee {
        display: block;
        position: relative;
        padding-left: 40px;
      }
    }
  }
</style>
